import Vue from 'vue'
const that = Vue.prototype
const touchAudioContext = uni.createInnerAudioContext()
const neutralAudioContext = uni.createInnerAudioContext()
const sucAudioContext = uni.createInnerAudioContext()
const losAudioContext = uni.createInnerAudioContext()
let dialogueAudioContext = uni.createInnerAudioContext()
let narratorAudioContext = uni.createInnerAudioContext()
touchAudioContext.src = "https://hz-huibenxia.oss-cn-shenzhen.aliyuncs.com/Static-library/music/%E8%A7%92%E8%89%B2.mp3"
neutralAudioContext.src = "http://hz-huibenxia.oss-cn-shenzhen.aliyuncs.com/Static-library/music/%E4%BB%BB%E5%8A%A1%E6%88%90%E5%8A%9F.mp3"
/** 
 * 播放点击音效
 * @method touchAudio()
 */
const touchAudio = () => {
	touchAudioContext.play()
}
/**
 * 播放音效 答题不判断对错时播放
 * @method neutralAudio() 
 */
const neutralAudio = () => {
	neutralAudioContext.play()
}
/**
 * 播放音效 答题成功时播放 从 correctAudioUrls 音频库中随机
 * @method sucAudio() 
 */
const sucAudio = () => {
	const random = Math.round(Math.random() * (that.correctAudioUrls.length - 1))
	sucAudioContext.src = encodeURI(that.correctAudioUrls[random])
	sucAudioContext.play()
}
/**
 * 播放音效 答题失败时播放 从 incorrectAudioUrls 音频库中随机
 * @method losAudio() 
 */
const losAudio = () => {
	const random = Math.round(Math.random() * (that.incorrectAudioUrls.length - 1))
	losAudioContext.src = encodeURI(that.incorrectAudioUrls[random])
	losAudioContext.play()
}
/**
 * 播放指定音频 旁白
 * @param {string} src  音频地址
 * @param {bool} [play] 是否播放   
 * @callback end()  	音频播放结束
 * @callback start() 	音频开始播放
 */
const dialogueAudio = (option) => {
	const resultOption = Object.assign({
		"src": "",
		"play": true,
		"end": null,
		"start": null
	}, option)
	if (resultOption.play) {
		dialogueAudioContext.destroy()
		dialogueAudioContext = uni.createInnerAudioContext()
		dialogueAudioContext.offPlay()
		dialogueAudioContext.offEnded()
		let [isEnd, isStart] = [false, false]
		dialogueAudioContext.src = resultOption.src
		setTimeout(()=>{
			dialogueAudioContext.play()
		},0)
		dialogueAudioContext.onPlay(() => {
			if (resultOption.start && !isStart) {
				resultOption.start()
				isStart = true
			}
		})
		dialogueAudioContext.onEnded(() => {
			if (resultOption.end && !isEnd) {
				resultOption.end()
				isEnd = true
			}
		})
	} else {
		dialogueAudioContext.src = null
		dialogueAudioContext.pause()
	}
}
/**
 * 播放指定音频 对白
 * @param {string} src  音频地址
 * @param {bool} [play] 是否播放   
 * @callback end()  	音频播放结束
 * @callback start() 	音频开始播放
 */
const narratorAudio = (option) => {
	const resultOption = Object.assign({
		"src": "",
		"play": true,
		"end": null,
		"start": null
	}, option)
	if (resultOption.play) {
		let [isEnd, isStart] = [false,false]
		narratorAudioContext.destroy()
		narratorAudioContext = uni.createInnerAudioContext()
		narratorAudioContext.offPlay()
		narratorAudioContext.offEnded()
		narratorAudioContext.src = resultOption.src
		narratorAudioContext.play()
		narratorAudioContext.onPlay(() => {
			if (resultOption.start && !isStart) {
				resultOption.start()
				isStart = true
			}
		})
		narratorAudioContext.onEnded(() => {
			if (resultOption.end && !isEnd) {
				resultOption.end()
				isEnd = true
			}
		})
	} else {
		narratorAudioContext.src = null
		narratorAudioContext.pause()
	}
}
/**
 * 文字转语音 
 * @param {string} text      转换的字符串
 * @param {string} [speaker] 转换后的音频发音人  
 * @param {string} [spped]   转换后的音频速度
 * @param {string} [pitch]   转换后的音频音调
 * @param {string} [volume]  转换后的音频音量大小  
 * @returns audioUrl
 */
const parseTextAudio = async (option) => {
	const resultOption = Object.assign({
		"text": "",
		"speaker": "Aiwei",
		"speed": -280,
		"pitch": 0,
		"volume": 100
	}, option)
	const [data, err] = await that.awaitWarp(that.$post(that.api.parseTextAudio, resultOption))
	if (data) return data.audioUrl
}
export default {
	touchAudio,
	neutralAudio,
	sucAudio,
	losAudio,
	dialogueAudio,
	narratorAudio,
	parseTextAudio
}
